package action;

import POJO.User;
import POJO.UserDao;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by hui on 2017/6/21.
 */
public class LoginAction extends ActionSupport {
    //两个属性名跟Login.jsp的form属性的name一样
    //这样struts2框架会自动复制，每个属性需要getter和setter
    private String username;
    private String pwd;
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    @Override
    public String execute() throws Exception {
        String forword = null;
        User userEntity =new User();
        userEntity.setName(username);
        userEntity.setPwd(pwd);
        //mybatis配置文件中设置了，如果没有查询到返回null
        User entity = userDao.checkUser(userEntity);

        if (entity!=null) {
            System.out.println(entity.toString());
            ActionContext.getContext().put("user",entity);
            forword = ActionSupport.SUCCESS;
        } else {
            System.out.println("no such user");
            forword = ActionSupport.INPUT;
        }
        return forword;
    }
}
